package com.hz.servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jspsmart.upload.File;
import com.jspsmart.upload.SmartUpload;

public class UpLoadUserHeadImage extends HttpServlet {
	/**
     * 
     */
    private static final long serialVersionUID = 1L;
	private ServletConfig config = null;
	private String FileName = null;
	private String sPath = "/resources/photos";

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		SmartUpload mySmartUpload = new SmartUpload();
		
		mySmartUpload.initialize(config, request, response);
		
		mySmartUpload.setMaxFileSize(2048 * 1024);
		mySmartUpload.setAllowedFilesList("jpg,gif,png,jpeg,bmp,JPG,JPEG,BMP,PNG,GIF");
		try {
			
			mySmartUpload.upload();
			
			File myFile = mySmartUpload.getFiles().getFile(0);
			
			if (!myFile.isMissing()) {
				
				Date currTime = new Date();
				SimpleDateFormat formatter2 = new SimpleDateFormat(
						"yyyyMMddhhmmssS", Locale.US);
				FileName = new String((formatter2.format(currTime))
						.getBytes("iso-8859-1"));
				String ext = myFile.getFileExt();
				FileName = FileName + "." + ext;
//				System.out.println(sPath);
//				System.out.println(FileName);
//				System.out.println(sPath + "/" + FileName);
				
				
				myFile.saveAs(sPath + "/" + FileName,mySmartUpload.SAVE_VIRTUAL);
				
			}
			response.sendRedirect("/HongZhu/pages/register/uploadimage.jsp?Picurl="
					+ FileName + " ");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void init(ServletConfig config) throws ServletException {
		this.config = config;
	}

}
